
'use strict';

const express=require('express');
const path=require('path');
const xtpl=require('xtpl');
const moment=require('moment');
const db=require(path.join(__dirname,"../configs/database.js"));
const base=require(path.join(__dirname,"./baseController.js"));

module.exports.indeAdd=function(req,res){

  // 反馈学生 反馈班级 反馈给教师
 db.table('student').select().then(function(sData){
     // 班级
     db.table('class').select().then(function(cData){
     	 //教师
     	db.table('teach').select().then(function(tData){ 
      xtpl.renderFile(
       path.join(__dirname,"../views/admin/ind_add.html"),
       {
        srr:sData,
        crr:cData,
        trr:tData
       },
       function(err,content){

          res.setHeader("Content-Type","text/html;charset=utf-8");
          res.end(content);

       }
    	);   
     	});
     });
 });
};

module.exports.indeAddData=function(req,res){

   let data={
     "stu_id":req.body.stu_id,
     "teach_id":req.body.teach_id,
     "class_id":req.body.class_id,
     "message_content":req.body.message_content,
     "message_time":(Date.parse(moment(req.body.message_time))/1000),
   };


   db.table('message').add(data).then(function(affectRows){

       res.redirect('/ind/ind_list');

       return false;

   }).catch(function(err){
         
         if(err){
         	return console.log(err);
         }


   });

};


module.exports.indeList=(req,res)=>{
const page=parseInt(req.query.page||'1');
const limit=parseInt(req.query.limit||'12');
const start=(page-1)*limit;
db.table('message').count("*").then(function(count){
  const totalPage=count%limit==0?count/limit:parseInt(count/limit)+1;
  const totalPageArray=[];
  for(var i=1;i<=totalPage;i++){
    totalPageArray.push(i);
  };
  db.table('message').join("pre_student on pre_message.stu_id=pre_student.stu_id").join("pre_teach on pre_message.teach_id=pre_teach.teach_id").join("pre_class on pre_message.class_id=pre_class.class_id").order("message_id DESC").limit(start,limit).select().then(function(data){
    data.message_time=moment(data.message_time*1000).format("YYYY-MM-DD");
   xtpl.renderFile(
   path.join(__dirname,"../views/admin/ind_list.html"),
   {
     mes:data,
     totalPage:totalPage,
     totalPageArray:totalPageArray,
     page:page
   },
   function(err,content){
       res.setHeader("Content-Type","text/html;charset=utf-8");
       res.end(content);
   }
  );
  });
});

};





module.exports.indEdite=(req,res)=>{
    

  var message_id=req.params.message_id||'1';
  var where=`message_id=${message_id}`;

  db.table('message').where(where).find().then(function(data){

  
       db.table('student').select().then(function(sData){

          db.table('teach').select().then(function(tData){

             db.table('class').select().then(function(cData){

                 data.message_time=moment(data.message_time*1000).format("YYYY-MM-DD");

                xtpl.renderFile(

                 path.join(__dirname,"../views/admin/ind_edite.html"),
                    {
       
                       arr:data,
                       srr:sData,
                       trr:tData,
                       crr:cData
             },
             (err,content)=>{

               res.setHeader("Content-Type","text/html;charset=utf-8");
               res.end(content);
             
              }
               );

             });

          });

       });
  });

};



module.exports.indEditeData=(req,res)=>{
  
  let message_id=req.params.message_id||0;
  let where=`message_id=${message_id}`;
  db.table('message').where(where).find().then(function(data){
   if(JSON.stringify(data)=="{}"){
       base.showMsg(res,"该反馈信息不存在","/ind/ind_list");
       return false;

   }else{
     let check=`message_content='${req.body.message_content}' AND message_id !=${message_id}`;
     db.table('message').where(check).find().then(function(data){
         if(JSON.stringify(data)!="{}"){
             base.showMsg(res,"该反馈信息已经存在","/ind/ind_list");
             return false;
         }else{
         let data={
         "stu_id":req.body.stu_id,
         "teach_id":req.body.teach_id,
         "class_id":req.body.class_id,
         "message_content":req.body.message_content,
         "message_time":(Date.parse(moment(req.body.message_time))/1000),
       };
           db.table('message').where(`message_id=${message_id}`).update(data).then(function(affectRows){
              res.redirect('/ind/ind_list');
              return false;
           }).catch(function(err){
             if(err){
              base.showMsg(res,'更新失败');
              return false;
             }

           });
         }

     });

   }
  });
  

};

module.exports.indDelete=(req,res)=>{
  let message_id=req.params.message_id;
  let where=`message_id=${message_id}`;

  db.table('message').where(where).delete().then((messageRows)=>{
     
      base.showMsg(res,'删除成功',"/ind/ind_list");
      return false;
    
  }).catch((err)=>{

     if(err){
        return console.log(err);

     }
     base.showMsg(res,'删除失败',"/ind/ind_list");
     return false;

  });


};